#include "control.h"
#include "oled.h"
#include "dinogame.h"
#include "stdlib.h"
#include "delay.h"
#include "usart.h"
unsigned char key_num = 0;
unsigned char cactus_category = 0; // 仙人掌类别
unsigned char cactus_length = 8;   // 仙人掌长度
unsigned int score = 0;					   // 分数
unsigned int highest_score = 0;	   // 最高分
int height = 0;										 // 跳跃高度
int cactus_pos = 128;							 // 仙人掌位置
int fly_dragon_pos = 128;					 // 飞龙位置
unsigned char fly_dragon_length = 16;   // 飞龙长度
unsigned char cur_speed = 30;			 // 速度
char failed = 0;									 // 游戏结束标志
char reset = 0;										 // 游戏重置标志
int flyingDragonPresent = 0;
int cactusPresent = 0;
void Game_control()
{
    while(1)
    {

        if(get_key() == 1)  break;		//按键按下强制退出一次循环即恐龙跳跃

				// 角色死亡
        if (failed == 1)
        {
            OLED_DrawRestart();
						// 重新游戏
					  key_num = get_key();
						if (key_num == 2)
						{
                // 重置游戏状态，清空分数，重置高度，设置游戏重置标志，并清空屏幕。
								printf("{\"code\":%d,\"data\":{\"sn\":%s,\"name\":\"%s\",\"value\":%d,\"unit\":\"%s\",\"status\":%s},\"msg\":\"%s\"}", 200, "1006", "game", score, "", "true", "success");
								delay_ms(200);
								if(score > highest_score) highest_score = score;
								score = 0;
								failed = 0;
								height = 0;
								reset = 1;
								OLED_DrawDinoJump(reset);
								//OLED_DrawCactusRandom(cactus_category, reset);
								fly_dragon_pos = OLED_DrawFlyDragon(reset);
								OLED_Clear();
						}
						continue;
        }


        score ++;
        if (height <= 0) key_num = get_key();

        OLED_DrawGround();
        OLED_DrawCloud();

        if (height > 0 || key_num == 2) height = OLED_DrawDinoJump(reset);
        else OLED_DrawDino();
				
				// 随机出现飞龙或者仙人掌，仙人掌出现概率大于飞龙，分数300以内不出现飞龙
        //if (score < 300 || rand() % 3 != 0) {
				/*if (rand() % 3 != 0 && !cactusPresent) {
            cactus_category = rand() % 4;
            cactus_pos = OLED_DrawCactusRandom(cactus_category, reset);
        } else if(rand() % 3 == 0 && !flyingDragonPresent){
            fly_dragon_pos = OLED_DrawFlyDragon(reset);
        }*/
				//fly_dragon_pos = OLED_DrawFlyDragon(reset);
        cactus_pos = OLED_DrawCactusRandom(cactus_category, reset);
				
        // 根据仙人掌的类别，设置仙人掌的长度。
        if(cactus_category == 0) cactus_length = 8;
        else if(cactus_category == 1) cactus_length = 16;
        else cactus_length = 24;

        // 如果仙人掌已经完全移出屏幕，则随机生成新的仙人掌。
        if (cactus_pos + cactus_length < 0)
        {
            cactus_category = rand() % 4;
            OLED_DrawCactusRandom(cactus_category, 1);
            //cactusPresent = 0;
        }
        // 如果飞龙已经完全移出屏幕，则随机生成新的飞龙。
        if (fly_dragon_pos + fly_dragon_length < 0)
        {
            OLED_DrawFlyDragon(1);
            //flyingDragonPresent = 0;
        }
				
        if (
					// 16表示8恐龙的一般+8仙人掌/飞龙的一半 24、32同理
					// 如果恐龙和飞龙碰撞，则设置游戏失败标志。
					((height < 16) && ((fly_dragon_pos >= 16 && fly_dragon_pos <= 24 ) || (fly_dragon_pos + fly_dragon_length >= 16 &&fly_dragon_pos + fly_dragon_length <= 24 )))
					// 如果恐龙和仙人掌碰撞,则设置游戏失败标志。
					|| ((height < 16) && ((cactus_pos >= 16 && cactus_pos <= 32) || (cactus_pos + cactus_length >= 16 && cactus_pos + cactus_length <= 32))))
        {
            failed = 1;
        }

        OLED_ShowString(35, 0, "HI:", 12);
        OLED_ShowNum(58, 0, highest_score, 5, 12);
        OLED_ShowNum(98, 0, score, 5, 12);


        reset = 0;

        cur_speed = score / 20;
        if (cur_speed > 29) cur_speed = 29;
        delay_ms(30 - cur_speed);
        key_num = 0;
    }
		
}
